package com.authine.cloudpivot.ext.controller.datadrying;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.authine.cloudpivot.engine.api.model.organization.DepartmentModel;
import com.authine.cloudpivot.ext.service.DataDryingService;
import com.authine.cloudpivot.web.api.controller.base.BaseController;
import com.authine.cloudpivot.web.api.dubbo.DubboConfigService;
import com.authine.cloudpivot.web.api.exception.ResultEnum;
import com.authine.cloudpivot.web.api.view.ResponseResult;
import com.taobao.api.ApiException;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 数据晾晒相关接口
 *
 * @author shinka
 */
@Api(value = "EXTAPI::数据晾晒相关接口", tags = "01::EXTAPI::数据晾晒相关接口")
@RestController
@RequestMapping("/ext/data/drying")
public class DataDryingController extends BaseController {
    private static Logger log = LoggerFactory.getLogger(DataDryingController.class);

    @Autowired
    private DataDryingService dataDryingService;

    @Autowired
    private DubboConfigService dubboConfigService;

    /**
     *
     *
     * @param deptId
     * @return
     */
    @ApiOperation("通过村级组织Id 循环获取村级下每个人员对应的信息 进行统计晾晒")
    @PostMapping(value = "/statistics")
    public ResponseResult<Object> statistics(@Param(value = "deptId") String deptId) throws ApiException {
        log.debug("statistics-deptId={}", deptId);
        if (StringUtils.isBlank(deptId)) {
            List<DepartmentModel> rootDepartment = dubboConfigService.getOrganizationFacade().getRootDepartment();
            for (DepartmentModel departmentModel : rootDepartment) {
                if (!departmentModel.getCorpId().equals("main")) {
                    dataDryingService.statistics(departmentModel.getId());
                }
            }
        } else {
            dataDryingService.statistics(deptId);
        }
        return getOkResponseResult("success " + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()),
            ResultEnum.SUCCESS.getErrMsg());
    }
}
